package cn.lijiajia3515.cairo.auth.framework.security;

import cn.lijiajia3515.cairo.auth.modules.auth.AuthType;
import cn.lijiajia3515.cairo.domain.CairoAccount;
import cn.lijiajia3515.cairo.security.authentication.CairoAuthentication;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;

/**
 * cairo 认证用户 对象
 */
@Getter
@Setter
@Accessors(chain = true)

@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder(toBuilder = true)
public class CairoAuthAccount extends CairoAuthentication implements UserDetails, CredentialsContainer, Serializable {

	private AuthType type;

	private String password;

	/**
	 * 账号是否过期
	 */
	@Builder.Default
	private boolean accountExpired = false;

	/**
	 * 账号是否锁定
	 */
	@Builder.Default
	private boolean accountLocked = false;

	/**
	 * 账号是否启用
	 */
	@Builder.Default
	private boolean accountEnabled = true;
	/**
	 * 凭证是否过期
	 */
	@Builder.Default
	private boolean credentialsExpired = false;

	@Override
	@JsonIgnore
	public String getUsername() {
		return getLogin();
	}

	@Override
	@JsonIgnore
	public String getPassword() {
		return this.password;
	}

	/**
	 * <p>账号是否过期</p>
	 * <p>永不过期</p>
	 *
	 * @return true
	 */
	@Override
	public boolean isAccountNonExpired() {
		return !accountExpired;
	}

	@Override
	public boolean isAccountNonLocked() {
		return !accountLocked;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return !credentialsExpired;
	}


	@Override
	public boolean isEnabled() {
		return accountEnabled;
	}

	@Override
	public void eraseCredentials() {
		this.password = null;
	}
}
